***************************************************************************************************************
* Name: Table_A5_inter
* Description: final dataset contains results reported in table A5
* Reference: "Competition and Civilian Victimization"
***************************************************************************************************************

use "violence_t_export.dta",clear
keep if victim_farc~=.&victim_paras~=.

*****Log likelihood definition*****

global vF "victim_farc"
global vP "victim_paras" 

*Victimization un-restricted
capture program drop lfsec_ur
program lfsec_ur
  version 13.1
  args lnf xbF xbP
  tempvar pF pP
  quietly {
     gen double `pF' = 1/(1+exp(-`xbF'))
     gen double `pP' = 1/(1+exp(-`xbP'))
     replace `lnf' = $vP* ln(`pP')+(1-$vP)*ln(1-`pP') + $vF*ln(`pF')+(1-$vF)*ln(1-`pF') 
   }
end


*Two stage estimation
 capture program drop game2stage
 program game2stage, eclass
	syntax [if]
	marksample touse
	tempname b b2 dF2 dP2
	tempvar predF predP
	local controls "nbi_t royalties_t coca_t share_left lpobl_tot_t i.time army_dist gini_i dmr evlp gcaribe gpacifica gorinoquia gamazonia"
	
	quietly {
		npregress kernel victim_farc `controls' farc_dist if victim_paras~=., estimator(constant) noderivatives
		predict `predF'
		npregress kernel victim_paras `controls' paras_dist if victim_paras~=.,estimator(constant) noderivatives
		predict `predP'
	}
	
	gen pF=`predF'
	gen pP=`predP'
		
	corr  pF victim_farc pP victim_paras
	ml model lf lfsec_ur (eq1: victim_farc = nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist c.army_dist#i.time  ///
						 dmr c.dmr#i.time gini_i  time evlp c.evlp#i.time gcaribe c.gcaribe#i.time gpacifica c.gpacifica#i.time gorinoquia c.gorinoquia#i.time gamazonia c.gamazonia#i.time farc_dist c.farc_dist#i.time pP) (eq2: nbi_t royalties_t coca_t ///
						share_left lpobl_tot_t army_dist c.army_dist#i.time dmr c.dmr#i.time gini_i time ///
						 evlp c.evlp#i.time gcaribe c.gcaribe#i.time gpacifica c.gpacifica#i.time gorinoquia c.gorinoquia#i.time gamazonia c.gamazonia#i.time paras_dist c.paras_dist#i.time pF)
	
	capture noisily ml maximize, diff 
	display "maximize" _rc
	if _rc~=0 {
		matrix `b'=J(1,4,0)
	}
	else{
		matrix `b'=e(b)

	}
	drop pF pP
	ereturn post `b' ,esample(`touse')		
	
end

game2stage
matrix b=(_b[eq1:farc_dist]\_b[eq1:pP]\_b[eq2:paras_dist]\_b[eq2:pF])'
matrix res_boot=b
set seed 1313

set matsize 800

forvalues i = 1/500{
	preserve
	*bsample ,cluster(coddepto)
	bsample
	game2stage
	matrix b=(_b[eq1:farc_dist]\_b[eq1:pP]\_b[eq2:paras_dist]\_b[eq2:pF])'
	matrix res_boot=(res_boot\b)
	display `i'
	restore
}

svmat double res_boot
keep res_boot1-res_boot4
save "boot_res_region_inter.dta",replace
use "boot_res_region_inter.dta",clear

keep res_boot1-res_boot4

forvalue i=1/4 {
	replace res_boot`i'=. if res_boot`i'==0
}	

tabstat res_boot1-res_boot4 if _n~=1,s(sd) save
matrix sd_mat=r(StatTotal)
keep if _n==1
gen dum=1
reshape long res_boot, i(dum) j(estimates)
drop dum estimates
rename res_boot estimates
matrix sd_mat=sd_mat'
svmat sd_mat

*Generating pvals and confidence intervals
gen pval=min(2*normal(estimates/sd_mat),2*(1-normal(estimates/sd_mat)))

list sd_mat
list pval
